package ljp.controller;
import ljp.entity.GoodsEntity;
import ljp.service.GoodsService;
import ljp.vo.ActionResult;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName Hello
 * @Description TODO
 * @Author LJP
 * @Date 2022/10/15 19:56
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    private final GoodsService goodsService;

    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }



    /**
    *
     * @Description: 获取全部商品信息
     * @param
     * @return: java.util.List<com.LJP.assignment4.entity.Goods>
     * @Author: LJP
     */
    @GetMapping()
    @Transactional
    public ActionResult hello(){
        List<GoodsEntity> goodsEntityList = goodsService.getAll();
        return ActionResult.success(goodsEntityList);

    }

    /**
    *
     * @Description: 获取指定商品信息
     * @param id 商品编号
     * @return:
     * @Author: LJP
     */
    @GetMapping("/getOne/{id}")
    @Transactional
    public ActionResult getGoodById(@PathVariable("id") String id){
        GoodsEntity goods = goodsService.getGoodById(id);
        if(null != goods){
            return ActionResult.success(goodsService);
        }
        else{
            return ActionResult.fail("查询失败，编号不存在");
        }
    }

    @ApiOperation("根据条件查询待办事项")
    @GetMapping("/getByName")
    @Transactional
    public ActionResult getGoodByName(@ApiParam("商品名称") String name){
        GoodsEntity goods = goodsService.getGoodByName(name);
        if(null != goods){
            return ActionResult.success(goodsService);
        }
        else{
            return ActionResult.fail("查询失败，商品不存在");
//            return ActionResult.fail(name);
        }
    }

    /**
    *
     * @Description: 更新商品全部信息
     * @return: 是否成功
     * @Author: LJP
     */
    @PutMapping("/updateAll")
    @Transactional
    public ActionResult updateAll(@RequestBody List<GoodsEntity> goodLists){
        if (goodLists == null){
            return ActionResult.success("所需更新数组为空");
        }
        for(GoodsEntity good:goodLists){
            String id = good.getId();
            if(!goodsService.updateOne(id,good)){
                return ActionResult.fail("更新失败");
            }
        }
        return ActionResult.success("更新成功！");
    }

    /**
    *
     * @Description: 更新某个商品信息
     * @param good 某个商品
     * @return: 是否成功
     * @Author: LJP
    */
    @PatchMapping("/updateOne/{id}")
    @Transactional
    public ActionResult updateOne(@PathVariable("id") String id,@RequestBody GoodsEntity good){
        if(goodsService.updateOne(id,good)){
            return ActionResult.success("更新成功！");
        }else{
            return ActionResult.fail("更新失败");
        }
    }

    /**
    *
     * @Description: 创建新的商品列表信息
     * @param goods
     * @return: vo.ljp.ActionResult
     * @Author: LJP
     */
    @PostMapping()
    @Transactional
    public ActionResult create(@RequestBody List<GoodsEntity> goods){
        for(GoodsEntity goodsEntity:goods){
            if(Boolean.FALSE.equals(goodsService.create(goodsEntity))){
                return ActionResult.fail("创建失败");
            }
        }
        return ActionResult.success("创建成功！");
    }

    @PostMapping("/createOne")
    @Transactional
    public ActionResult create(@RequestBody GoodsEntity good){

            if(Boolean.FALSE.equals(goodsService.create(good))){
                return ActionResult.fail("创建失败");
            }

        return ActionResult.success("创建成功！");
    }

    /**
    *
     * @Description: 删除某个特定的商品
     * @param good 需要删除的商品
     * @return: vo.ljp.ActionResult
     * @Author: LJP
     */
    @DeleteMapping("/deleteOne")
    public ActionResult deleteById(@RequestBody String id){
//        String id = good.getId();
        if(Boolean.TRUE.equals(goodsService.deleteById(id))){
            return ActionResult.success("删除成功！");
        }
        else{
            return ActionResult.fail("删除失败！");
        }
    }

    /**
    *
     * @Description: 删除商品列表
     * @param goodsEntityList 需要删除的商品列表
     * @return: vo.ljp.ActionResult
     * @Author: LJP
     */
    @DeleteMapping("/deleteList")
    public ActionResult delete(@RequestBody List<GoodsEntity> goodsEntityList){
        for(GoodsEntity good:goodsEntityList){
            String id = good.getId();
            if(Boolean.FALSE.equals(goodsService.deleteById(id))){
                return ActionResult.fail("删除失败");
            }
        }
        return ActionResult.success("删除成功！");
    }
}
